package net.tngou.iask.action.my;

import org.apache.commons.lang3.math.NumberUtils;

import net.tngou.iask.action.BaseAction;
import net.tngou.iask.jdbc.PageUtil;
import net.tngou.iask.pojo.Answer;
import net.tngou.iask.service.AnswerService;

/**
 * 问题选项
 * @author tngou@tngou.net
 *
 */
public class AnswerAction extends BaseAction{

	/**
	 * 列表
	 */
	public void list() {
		String[] params = request.getParams();
		int type=0;
		if(params!=null&&params.length>0&&NumberUtils.isNumber(params[0]))
		{	
			type=Integer.parseInt(params[0]);
			
		}
		if(user.getRole()!=1&&user.getRole()==2&&user.getRole()==3)
		{
			run_500();return;
		}
		AnswerService service = new AnswerService();
		PageUtil page = service.getPage(ask.getPage(), ask.getSize(), user.getMain(), type, ask.getKeyword());
		root.put("page", page);
		root.put("type", type);
		root.put("keyword", ask.getKeyword());
		printFreemarker("my/answer_list.ftl", root);
	}
	
	public void add() {
		String[] params = request.getParams();
		int type=0;
		if(params!=null&&params.length>0&&NumberUtils.isNumber(params[0]))
		{	
			type=Integer.parseInt(params[0]);
		}
		if(user.getRole()!=1&&user.getRole()==2&&user.getRole()==3)
		{
			run_500();return;
		}
		root.put("type", type);
		printFreemarker("my/answer_add.ftl", root);
	}
	/**
	 * 更新
	 */
	public void update() {
		String[] params = request.getParams();
		int id=0;
		if(params!=null&&params.length>0&&NumberUtils.isNumber(params[0]))
		{	
			id=Integer.parseInt(params[0]);
			
		}
		if(id==0){run_404();return;}
		Answer answer=new Answer().get(id);
		if(answer==null||answer.getMain()!=user.getMain()){run_500();return;}
		root.put("answer", answer);
		printFreemarker("my/answer_update.ftl", root);
	}
}
